﻿using System;

namespace ExFlash.Tags
{
    public class SoundStreamHeadTag : Tag
    {
        public SoundRates PlaybackSoundRate
        {
            get;
            protected set;
        }

        public SoundSizes PlaybackSoundSize
        {
            get;
            protected set;
        }

        public SoundTypes PlaybackSoundType
        {
            get;
            protected set;
        }

        public AudioCodingFormat StreamSoundCompression
        {
            get;
            protected set;
        }

        public SoundRates StreamSoundRate
        {
            get;
            protected set;
        }

        public virtual SoundSizes StreamSoundSize
        {
            get;
            protected set;
        }

        public SoundTypes StreamSoundType
        {
            get;
            protected set;
        }

        public ushort StreamSoundSampleCount
        {
            get;
            protected set;
        }

        public short LatencySeek
        {
            get;
            protected set;
        }

        public SoundStreamHeadTag(int len, int offset)
            : this(TagCodes.SoundStreamHead, len, offset)
        {

        }

        protected SoundStreamHeadTag(TagCodes code, int len, int offset)
            : base(code, len, offset)
        {

        }

        public override void SwfDeserialize(BinaryBitReader reader)
        {
            // Reserved
            reader.SkipBits(4);

            PlaybackSoundRate = (SoundRates)reader.ReadUInt32(2);
            
            PlaybackSoundSize = (SoundSizes)reader.ReadUInt32(1);

            PlaybackSoundType = (SoundTypes)reader.ReadUInt32(1);

            StreamSoundCompression = (AudioCodingFormat)reader.ReadUInt32(4);

            StreamSoundRate = (SoundRates)reader.ReadUInt32(2);

            // StreamSoundSize always 1.
            StreamSoundSize = (SoundSizes)reader.ReadUInt32(1);

            StreamSoundType = (SoundTypes)reader.ReadUInt32(1);

            reader.AlignBits();
            
            StreamSoundSampleCount = reader.ReadUInt16();

            if (StreamSoundCompression == AudioCodingFormat.MP3)
            {
                LatencySeek = reader.ReadInt16();
            }
        }

        public override void SwfSerialize(byte[] arr, ref int offset)
        {
            throw new NotImplementedException();
        }
    }
}
